﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CVCore32.Class.Funciones.Productos
{
    public class Categoria
    {

        public static List<Data.CategoriaProducto> GetCategorias()
        {
            try
            {
                CVCore32.Class.Core.Sesion sesion = CVCore32.Class.Core.Sesion.GetInstace;
                Data.electrondbEntities context;
                if (sesion.ServidorWeb)
                {
                    context = new Data.electrondbEntities("Data Source=72.18.135.49, 1533;Initial Catalog=electrondb;User Id=JotaEmePM;Password=ez881010;");
                }
                else
                {
                    context = new Data.electrondbEntities();
                }

                List<Data.CategoriaProducto> sc = (from cat in context.CategoriaProducto
                                                   orderby cat.IdCategoria ascending
                                                   select cat).ToList();
                return sc;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static List<Data.SubcategoriaProducto> GetSubCategorias()
        {
            try
            {
                CVCore32.Class.Core.Sesion sesion = CVCore32.Class.Core.Sesion.GetInstace;
                Data.electrondbEntities context;
                if (sesion.ServidorWeb)
                {
                    context = new Data.electrondbEntities("Data Source=72.18.135.49, 1533;Initial Catalog=electrondb;User Id=JotaEmePM;Password=ez881010;");
                }
                else
                {
                    context = new Data.electrondbEntities();
                }

                List<Data.SubcategoriaProducto> sc = (from cat in context.SubcategoriaProducto
                                                      orderby cat.IdSubcategoria ascending
                                                      select cat).ToList();
                return sc;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static int GetFirstItemCategoria()
        {
            try
            {
                CVCore32.Class.Core.Sesion session = CVCore32.Class.Core.Sesion.GetInstace;
                Data.electrondbEntities context;
                if (session.ServidorWeb)
                    context = new Data.electrondbEntities("");
                else
                    context = new Data.electrondbEntities();

                var num = (from cat in context.CategoriaProducto
                           orderby cat.IdCategoria ascending
                           select cat.IdCategoria).Single();
                return Convert.ToInt32(num);
	
            }
            catch (Exception)
            {

                return 0;
            }
        }

        public static List<Data.SubcategoriaProducto> GetSubCategorias(int Categoria)
        {
            try
            {
                CVCore32.Class.Core.Sesion sesion = CVCore32.Class.Core.Sesion.GetInstace;
                Data.electrondbEntities context;
                if (sesion.ServidorWeb)
                {
                    context = new Data.electrondbEntities("Data Source=72.18.135.49, 1533;Initial Catalog=electrondb;User Id=JotaEmePM;Password=ez881010;");
                }
                else
                {
                    context = new Data.electrondbEntities();
                }

                List<Data.SubcategoriaProducto> sc = (from cat in context.SubcategoriaProducto
                                                      where cat.IdCategoria == Categoria
                                                      select cat).ToList();
                return sc;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static bool ExisteCategoria(string nombre)
        {
            if (!String.IsNullOrWhiteSpace(nombre))
            {
                CVCore32.Class.Core.Sesion sesion = CVCore32.Class.Core.Sesion.GetInstace;
                Data.electrondbEntities context;
                if (sesion.ServidorWeb)
                {
                    context = new Data.electrondbEntities("Data Source=72.18.135.49, 1533;Initial Catalog=electrondb;User Id=JotaEmePM;Password=ez881010;");
                }
                else
                {
                    context = new Data.electrondbEntities();
                }

                var categoria = (from cat in context.CategoriaProducto
                                 where cat.NombreCategoria == nombre
                                 select cat.IdCategoria).Count();


                if (categoria != 0)
                    return true;
                else
                    return false;
            }
            else
            {
                return false;
            }
        }

        public static bool ExisteSubCategoria(string nombre, int categoria)
        {
            if (!String.IsNullOrWhiteSpace(nombre))
            {
                CVCore32.Class.Core.Sesion sesion = CVCore32.Class.Core.Sesion.GetInstace;
                Data.electrondbEntities context;
                if (sesion.ServidorWeb)
                {
                    context = new Data.electrondbEntities("Data Source=72.18.135.49, 1533;Initial Catalog=electrondb;User Id=JotaEmePM;Password=ez881010;");
                }
                else
                {
                    context = new Data.electrondbEntities();
                }

                var Subcategoria = (from cat in context.SubcategoriaProducto
                                    where 
                                    cat.NombreSubcategoria == nombre &&
                                    cat.IdCategoria == categoria
                                    select cat.IdCategoria).Count();


                if (Subcategoria != 0)
                    return true;
                else
                    return false;
            }
            else
            {
                return false;
            }
        }

        public static void CrearCategoria(string nombre)
        {
            CVCore32.Class.Core.Sesion sesion = CVCore32.Class.Core.Sesion.GetInstace;

            if (sesion.ServidorWeb)
            {
                using (Data.electrondbEntities context = new Data.electrondbEntities("Data Source=72.18.135.49, 1533;Initial Catalog=electrondb;User Id=JotaEmePM;Password=ez881010;"))
                {
                    context.Connection.Open();
                    using (var transaction = context.Connection.BeginTransaction())
                    {
                        try
                        {
                            Data.CategoriaProducto cat = new Data.CategoriaProducto()
                            {
                                NombreCategoria = nombre
                            };

                            context.AddToCategoriaProducto(cat);
                            context.SaveChanges();
                            transaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            throw new Exception(ex.InnerException.ToString());
                        }
                    }
                }
            }
            else
            {
                using (Data.electrondbEntities context = new Data.electrondbEntities())
                {
                    context.Connection.Open();
                    using (var transaction = context.Connection.BeginTransaction())
                    {
                        try
                        {
                            Data.CategoriaProducto cat = new Data.CategoriaProducto()
                            {
                                NombreCategoria = nombre
                            };

                            context.AddToCategoriaProducto(cat);
                            context.SaveChanges();
                            transaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            throw new Exception(ex.InnerException.ToString());
                        }
                    }
                }
            }
        }

        public static void CrearSubCategoria(string nombre, int categoria)
        {
            CVCore32.Class.Core.Sesion sesion = CVCore32.Class.Core.Sesion.GetInstace;

            if (sesion.ServidorWeb)
            {
                using (Data.electrondbEntities context = new Data.electrondbEntities("Data Source=72.18.135.49, 1533;Initial Catalog=electrondb;User Id=JotaEmePM;Password=ez881010;"))
                {
                    context.Connection.Open();
                    using (var transaction = context.Connection.BeginTransaction())
                    {
                        try
                        {
                            Data.SubcategoriaProducto cat = new Data.SubcategoriaProducto()
                            {
                                NombreSubcategoria = nombre,
                                IdCategoria = categoria
                            };

                            context.AddToSubcategoriaProducto(cat);
                            context.SaveChanges();
                            transaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            throw new Exception(ex.InnerException.ToString());
                        }
                    }
                }
            }
            else
            {
                using (Data.electrondbEntities context = new Data.electrondbEntities())
                {
                    context.Connection.Open();
                    using (var transaction = context.Connection.BeginTransaction())
                    {
                        try
                        {
                            Data.SubcategoriaProducto cat = new Data.SubcategoriaProducto()
                            {
                                NombreSubcategoria = nombre,
                                IdCategoria = categoria
                            };

                            context.AddToSubcategoriaProducto(cat);
                            context.SaveChanges();
                            transaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            throw new Exception(ex.InnerException.ToString());
                        }
                    }
                }
            }
        }
    }
}
